package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.opentides.util.StringUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.service.PatientService;

/**
 * 
 * @author Ashley
 *
 */
public class PatientProfileTabsController extends AbstractController{
	private PatientService patientService;
	private String ailmentsView;
	private String treatmentsView;
	private String programsView;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		
		
		String tab = request.getParameter("tab");
		if(!StringUtil.isEmpty(tab)){
			
			Patient patient = processPatient(request.getParameter("id"));
			if(patient!=null){
				
				map.put("patient", patient);
				
				if(tab.equalsIgnoreCase("treatments")){					
					return new ModelAndView(treatmentsView, map);
				} else if(tab.equalsIgnoreCase("programs")){
					return new ModelAndView(programsView, map);
				}
			}
		}
		
		return null;	//this is the error page
	}


	public PatientService getPatientService() {
		return patientService;
	}


	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}
	
	
	private Patient processPatient(String patientId){
		Patient patient = null;
		if(!StringUtil.isEmpty(patientId) && StringUtils.isNumeric(patientId)){
			patient = patientService.load(patientId);
		}
		return patient;
	}


	public String getAilmentsView() {
		return ailmentsView;
	}


	public void setAilmentsView(String ailmentsView) {
		this.ailmentsView = ailmentsView;
	}


	public String getTreatmentsView() {
		return treatmentsView;
	}


	public void setTreatmentsView(String treatmentsView) {
		this.treatmentsView = treatmentsView;
	}


	public String getProgramsView() {
		return programsView;
	}


	public void setProgramsView(String programsView) {
		this.programsView = programsView;
	}
}
